using Common.Communications;
using Common.Model;
using Microsoft.AspNetCore.Mvc;

namespace OpticalComService.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class WeatherForecastController : ControllerBase
    {
        private static readonly string[] Summaries = new[]
        {
            "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
        };

        private readonly ILogger<WeatherForecastController> _logger;
        ModbusCommunication modbusCommunication = new ModbusCommunication("127.0.0.1", 502);

        TcpCommunication tcpCommunication = new TcpCommunication();
        bool flag = false;
        public WeatherForecastController(ILogger<WeatherForecastController> logger)
        {
            _logger = logger;
        }

        [HttpGet(Name = "GetWeatherForecast")]
        public async Task<IEnumerable<WeatherForecast>> Get()
        {
            if (flag)
            {

                tcpCommunication.SendAsync("456");
            }
            //var flag = await modbusCommunication.ConnectAsync();
            //if (flag)
            //{
            //    var ss = modbusCommunication.ReadHoldingRegisters(1, 0, 4);
            //}

            return Enumerable.Range(1, 5).Select(index => new WeatherForecast
            {
                Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
                TemperatureC = Random.Shared.Next(-20, 55),
                Summary = Summaries[Random.Shared.Next(Summaries.Length)]
            })
            .ToArray();
        }
    }
}
